<?php

namespace Core;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Layout
 *
 * @author Admin
 */
class Layout
{
	protected $_vars = array();
	
	protected $_configs = array();
	
	protected $_widgets = array();

	public function __construct($configs)
	{
		$this->_configs = $configs;
	}

	public function assign($key, $val)
	{
		$this->_vars[$key] = $val;
	}
	
	public function fetch($file)
	{
		$path = $file;
		if(!file_exists($path))
			$path = rtrim($this->_configs['path'], '/') .'/'. ltrim($path, '/');
			
		ob_start();
		
		require $path;
		
		return ob_get_clean();
	}
	
	public function __get($key)
	{
		return $this->_vars[$key];
	}
	
	public function __call($func, $args)
	{
		$class = "\\Widgets\\" . ucfirst($func);
		
		$obj = isset($this->_widgets[$class]) ? $this->_widgets[$class] : null;

		if(!$obj)
		{
			$obj = new $class;

			$obj->setConfigs($this->_configs, $this);
			$obj->setVars($this->_vars);
			
			$this->_widgets[$class] = $obj;
		}
			
		return call_user_func_array(array($obj, $func), $args);
	}

	public function display($file)
	{		
		$file = $this->_configs['path'] .'/'. $file;
		
		if(!file_exists($file))
			return false;
		
		ob_start();
		
		require $file;
		
		echo ob_get_clean();
	}
	
	
}
